<!DOCTYPE html>
<head>
    <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="You can generate a PDF from your Jekyll project. You do this by creating a web version of your project that is printer friendly. You then use utility called ...">
<meta name="keywords" content="publishingsingle_sourcingcontent_types,  PDF, prince, prince XML, ant, xsl fo">
<title>Generating PDFs | 常见故障解决方案</title>
<link rel="stylesheet" href="css/syntax.css">


<link rel="stylesheet" type="text/css" href="css/font-awesome.min-4.5.0.css">
<!--<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">-->
<link rel="stylesheet" href="css/modern-business.css">
<link rel="stylesheet" href="css/lavish-bootstrap.css">
<link rel="stylesheet" href="css/customstyles.css">
<link rel="stylesheet" href="css/theme-blue.css">


<script type="text/javascript" charset="utf8" src="js/jquery.js"></script>
<script src="js/jquery.cookie.min.js"></script>
<script src="js/jquery.navgoco.min.js"></script>


<script src="js/bootstrap.min.js"></script>
<script src="js/anchor.min.js"></script>
<script src="js/toc.js"></script>
<script src="js/customscripts.js"></script>

<link rel="shortcut icon" href="images/favicon.ico">

<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://cdn.bootcss.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://cdn.bootcss.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->

<link rel="alternate" type="application/rss+xml" title="" href="feed.xml">

    <script>
        $(document).ready(function() {
            // Initialize navgoco with default options
            $("#mysidebar").navgoco({
                caretHtml: '',
                accordion: true,
                openClass: 'active', // open
                save: false, // leave false or nav highlighting doesn't work right
                cookie: {
                    name: 'navgoco',
                    expires: false,
                    path: '/'
                },
                slide: {
                    duration: 400,
                    easing: 'swing'
                }
            });

            $("#collapseAll").click(function(e) {
                e.preventDefault();
                $("#mysidebar").navgoco('toggle', false);
            });

            $("#expandAll").click(function(e) {
                e.preventDefault();
                $("#mysidebar").navgoco('toggle', true);
            });

        });

    </script>
    <script>
        $(function () {
            $('[data-toggle="tooltip"]').tooltip()
        })
    </script>

    

</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container topnavlinks">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="fa fa-home fa-lg navbar-brand" href="index.html">&nbsp;<span class="projectTitle"> 常见故障解决方案</span></a>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <!-- entries without drop-downs appear here -->
                
                
                
                <li><a href="index.html">重新搜索</a></li>
                
                
                
                <!-- entries with drop-downs appear here -->
                <!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
                
                
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown">其他文档入口<b class="caret"></b></a>
                    <ul class="dropdown-menu">
                        
                        
                        <li><a href="index.html">常见故障解决方案</a></li>
                        
                        
                        
                        <li><a href="p1_sample1.html">常用语法</a></li>
                        
                        
                    </ul>
                </li>
                
                
                
			<li><a class="email" title="问题反馈" href="#" onclick="javascript:window.location='mailto:Merlin_Wu@apllogistics.com?subject=《常见故障解决方案》反馈&body=%0D____________________________________________________________________%0D问题页面：%09Generating PDFs %0D问题地址：%09' + window.location.href;"><i class="fa fa-envelope-o"></i> 邮件反馈</a><li>

		
                <!--comment out this block if you want to hide search-->
                <li>
                    <!--start search-->
                    <!-- <div id="search-demo-container">
                        <input type="text" id="search-input" placeholder="search...">
                        <ul id="results-container"></ul>
                    </div>
                    <script src="js/jekyll-search.js" type="text/javascript"></script>
                    <script type="text/javascript">
                            SimpleJekyllSearch.init({
                                searchInput: document.getElementById('search-input'),
                                resultsContainer: document.getElementById('results-container'),
                                dataSource: 'search.json',
                                searchResultTemplate: '<li><a href="{url}" title="Generating PDFs">{title}</a></li>',
                    noResultsText: 'No results found.',
                            limit: 10,
                            fuzzy: true,
                    })
                    </script> -->
                    <!--end search-->
                </li>
            </ul>
        </div>
        </div>
        <!-- /.container -->
</nav>

<!-- Page Content -->
<div class="container">
    <div class="col-lg-12">&nbsp;</div>
    <!-- Content Row -->
    <div class="row">
        <!-- Sidebar Column -->
        <div class="col-md-3">

          












<!--  -->


<ul id="mysidebar" class="nav">
    <li class="sidebarTitle">目录 </li>
    
    
    
        
    
    <li>
        <a href="#">常用操作</a>
        <ul>
            
            
            
            <li><a href="index.html">搜索</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_introduction.html">Introduction</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_supported_features.html">Supported features</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_about.html">About the theme author</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_support.html">Support</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Release Notes</a>
        <ul>
            
            
            
            <li><a href="mydoc_release_notes_60.html">6.0 Release notes</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_release_notes_50.html">5.0 Release notes</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Installation</a>
        <ul>
            
            
            
            <li><a href="mydoc_about_ruby_gems_etc.html">About Ruby, Gems, Bundler, etc.</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_install_jekyll_on_mac.html">Install Jekyll on Mac</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_install_jekyll_on_windows.html">Install Jekyll on Windows</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Authoring</a>
        <ul>
            
            
            
            <li><a href="mydoc_pages.html">Pages</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_posts.html">Posts</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_lists.html">Lists</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_conditional_logic.html">Conditional logic</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_content_reuse.html">Content reuse</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_collections.html">Collections</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_webstorm_text_editor.html">WebStorm editor tips</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_atom_text_editor.html">Atom editor tips</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Navigation</a>
        <ul>
            
            
            
            <li><a href="mydoc_sidebar_navigation.html">Sidebar navigation</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_yaml_tutorial.html">YAML tutorial in the context of Jekyll</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_tags.html">Tags</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_series.html">Series</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Formatting</a>
        <ul>
            
            
            
            <li><a href="mydoc_adding_tooltips.html">Tooltips</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_alerts.html">Alerts</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_icons.html">Icons</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_images.html">Images</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_code_samples.html">Code samples</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_labels.html">Labels</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_hyperlinks.html">Links</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_navtabs.html">Navtabs</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_tables.html">Tables</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_syntax_highlighting.html">Syntax highlighting</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_workflow_maps.html">Workflow maps</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Handling reviews</a>
        <ul>
            
            
            
            <li><a href="mydoc_commenting_on_files.html">Commenting on files</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Publishing</a>
        <ul>
            
            
            
            <li><a href="mydoc_build_arguments.html">Build arguments</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_themes.html">Themes</a></li>
            
            
            
            
            
            
            <li class="active"><a href="mydoc_generating_pdfs.html">Generating PDFs</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_help_api.html">Help APIs and UI tooltips</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_search_configuration.html">Search configuration</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_iterm_profiles.html">iTerm profiles</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_push_build_to_server.html">Pushing builds to server</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_no_password_prompts_scp.html">Getting around the password prompts in SCP</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_publishing_github_pages.html">Publishing on Github Pages</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Special layouts</a>
        <ul>
            
            
            
            <li><a href="mydoc_kb_layout.html">Knowledge-base layout</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_glossary.html">Glossary layout</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_faq_layout.html">FAQ layout</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_scroll.html">Scroll layout</a></li>
            
            
            
            
            
            
            <li><a href="mydoc_shuffle.html">Shuffle layout</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Troubleshooting</a>
        <ul>
            
            
            
            <li><a href="mydoc_troubleshooting.html">Troubleshooting</a></li>
            
            
            
            
        </ul>
        
        
    
    <li>
        <a href="#">Tag archives</a>
        <ul>
            
            
            
            <li><a href="mydoc_tag_archives_overview.html">Tag archives overview</a></li>
            
            
            
            <li class="subfolders">
                <a href="#">Tag archive pages</a>
                <ul>
                    
                    
                    
                    <li><a href="tag_formatting.html">Formatting pages</a></li>
                    
                    
                    
                    
                    
                    <li><a href="tag_navigation.html">Navigation pages</a></li>
                    
                    
                    
                    
                    
                    <li><a href="tag_content_types.html">Content types pages</a></li>
                    
                    
                    
                    
                    
                    <li><a href="tag_publishing.html">Publishing pages</a></li>
                    
                    
                    
                    
                    
                    <li><a href="tag_special_layouts.html">Special layout pages</a></li>
                    
                    
                    
                    
                    
                    <li><a href="tag_collaboration.html">Collaboration pages</a></li>
                    
                    
                    
                    
                    
                    <li><a href="tag_troubleshooting.html">Troubleshooting pages</a></li>
                    
                    
                    
                </ul>
            </li>
            
            
            
            
        </ul>
        
        
        
        <!-- if you aren't using the accordion, uncomment this block:
           <p class="external">
               <a href="#" id="collapseAll">Collapse All</a> | <a href="#" id="expandAll">Expand All</a>
           </p>
           -->
    </li>
</ul>
</div>

<!-- this highlights the active parent class in the navgoco sidebar. this is critical so that the parent expands when you're viewing a page. This must appear below the sidebar code above. Otherwise, if placed inside customscripts.js, the script runs before the sidebar code runs and the class never gets inserted.-->
<script>$("li.active").parents('li').toggleClass("active");</script>

    <!-- Content Column -->
    <div class="col-md-9">
        <div class="post-header">
   <h1 class="post-title-main">Generating PDFs</h1>
</div>



<div class="post-content">

   
    <div class="summary">You can generate a PDF from your Jekyll project. You do this by creating a web version of your project that is printer friendly. You then use utility called Prince to iterate through the pages and create a PDF from them. It works quite well and gives you complete control to customize the PDF output through CSS, including page directives and dynamic tags from Prince.</div>
   

    
    
<!-- this handles the automatic toc. use ## for subheads to auto-generate the on-page minitoc. if you use html tags, you must supply an ID for the heading element in order for it to appear in the minitoc. -->
<script>
$( document ).ready(function() {
  // Handler for .ready() called.

$('#toc').toc({ minimumHeaders: 0, listType: 'ul', showSpeed: 0, headers: 'h2,h3,h4' });

/* this offset helps account for the space taken up by the floating toolbar. */
$('#toc').on('click', 'a', function() {
  var target = $(this.getAttribute('href'))
    , scroll_target = target.offset().top

  $(window).scrollTop(scroll_target - 10);
  return false
})
  
});
</script>

<div id="toc"></div>

    

    

    

    

  <h2 id="pdf-overview">PDF overview</h2>
<p>This process for creating a PDF relies on Prince XML to transform the HTML content into PDF. Prince costs about $500 per license. That might seem like a lot, but if you’re creating a PDF, you’re probably working for a company that sells a product, so you likely have access to some resources.</p>

<p>The basic approach is to generate a list of all pages that need to be added to the PDF, and then add leverage Prince to package them up into a PDF.</p>

<p>It may seem like the setup is somewhat cumbersome, but it doesn’t take long. Once you set it up, building a pdf is just a matter of running a couple of commands.</p>

<p>Also, creating a PDF this way gives you a lot more control and customization capabilities than with other methods for creating PDFs. If you know CSS, you can entirely customize the output.</p>

<h2 id="demo">Demo</h2>

<p>You can see an example of the finished product here:</p>

<p><a target="_blank" class="noCrossRef" href="pdf/mydoc.pdf"><button type="button" class="btn btn-default" aria-label="Left Align"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> PDF Download</button></a></p>

<h2 id="set-up-prince">1. Set up Prince</h2>

<p>Download and install <a href="http://www.princexml.com/doc/installing/">Prince</a>.</p>

<p>You can install a fully functional trial version. The only difference is that the title page will have a small Prince PDF watermark.</p>

<h2 id="create-a-new-configuration-file-for-each-of-your-pdf-targets">2. Create a new configuration file for each of your PDF targets</h2>

<p>The PDF configuration file will build on the settings in the regular configuration file but will some additional fields. Here’s the configuration file for the mydoc product within this theme. This configuration file is located in the pdfconfigs folder.</p>

<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="s">destination</span><span class="pi">:</span> <span class="s">_site/</span>
<span class="s">url</span><span class="pi">:</span> <span class="s2">"</span><span class="s">http://127.0.0.1:4010"</span>
<span class="s">baseurl</span><span class="pi">:</span> <span class="s2">"</span><span class="s">/mydoc-pdf"</span>
<span class="s">port</span><span class="pi">:</span> <span class="s">4010</span>
<span class="s">output</span><span class="pi">:</span> <span class="s">pdf</span>
<span class="s">product</span><span class="pi">:</span> <span class="s">mydoc</span>
<span class="s">print_title</span><span class="pi">:</span> <span class="s">Jekyll theme for documentation — mydoc product</span>
<span class="s">print_subtitle</span><span class="pi">:</span> <span class="s">version 5.0</span>
<span class="s">output</span><span class="pi">:</span> <span class="s">pdf</span>
<span class="s">defaults</span><span class="pi">:</span>
  <span class="pi">-</span>
    <span class="s">scope</span><span class="pi">:</span>
      <span class="s">path</span><span class="pi">:</span> <span class="s2">"</span><span class="s">"</span>
      <span class="s">type</span><span class="pi">:</span> <span class="s2">"</span><span class="s">pages"</span>
    <span class="s">values</span><span class="pi">:</span>
      <span class="s">layout</span><span class="pi">:</span> <span class="s2">"</span><span class="s">page_print"</span>
      <span class="s">comments</span><span class="pi">:</span> <span class="s">true</span>
      <span class="s">search</span><span class="pi">:</span> <span class="s">true</span>
</code></pre>
</div>

<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> Although you’re creating a PDF, you must still build an HTML web target before running Prince. Prince will pull from the HTML files and from the file-list for the TOC.</div>

<p>Note that the default page layout specified by this configuration file is <code class="highlighter-rouge">page_print</code>. This layout strips out all the sections that shouldn’t appear in the print PDF, such as the sidebar and top navigation bar.</p>

<p>Also note that there’s a <code class="highlighter-rouge">output: pdf</code> toggle in case you want to make some of your content unique to PDF output. For example, you could add conditional logic that checks whether <code class="highlighter-rouge">site.output</code> is <code class="highlighter-rouge">pdf</code> or <code class="highlighter-rouge">web</code>. If it’s <code class="highlighter-rouge">pdf</code>, then include information only for the PDF, and so on. If you’re using nav tabs, you’ll definitely want to create an alternative experience in the PDF.</p>

<p>In the configuration file, customize the values for the <code class="highlighter-rouge">print_title</code> and <code class="highlighter-rouge">print_subtitle</code> that you want. These will appear on the title page of the PDF.</p>

<h2 id="make-sure-your-sidebardocyml-file-has-a-titlepagehtml-and-tocpagehtml">3. Make sure your sidebar_doc.yml file has a titlepage.html and tocpage.html</h2>

<p>There are two template pages in the root directory that are critical to the PDF:</p>

<ul>
  <li>titlepage.html</li>
  <li>tocpage.html</li>
</ul>

<p>These pages should appear in your sidebar YML file (in this product, mydoc_sidebar.yml):</p>

<div class="language-yaml highlighter-rouge"><pre class="highlight"><code>  <span class="pi">-</span> <span class="s">title</span><span class="pi">:</span>
    <span class="s">output</span><span class="pi">:</span> <span class="s">pdf</span>
    <span class="s">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
    <span class="s">folderitems</span><span class="pi">:</span>
    <span class="pi">-</span> <span class="s">title</span><span class="pi">:</span>
      <span class="s">url</span><span class="pi">:</span> <span class="s">/titlepage/</span>
      <span class="s">output</span><span class="pi">:</span> <span class="s">pdf</span>
      <span class="s">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
    <span class="pi">-</span> <span class="s">title</span><span class="pi">:</span>
      <span class="s">url</span><span class="pi">:</span> <span class="s">/tocpage/</span>
      <span class="s">output</span><span class="pi">:</span> <span class="s">pdf</span>
      <span class="s">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
</code></pre>
</div>

<p>Leave these pages here in your sidebar. (The <code class="highlighter-rouge">output: pdf</code> property means they won’t appear in your online TOC because the conditional logic of the sidebar.html checks whether <code class="highlighter-rouge">web</code> is equal to <code class="highlighter-rouge">pdf</code> or not before including the item in the web version of the content.)</p>

<p>The code in the tocpage.html is mostly identical to that of the sidebar.html page. This is essential for Prince to create the page numbers correctly with cross references.</p>

<p>There’s another file (in the root directory of the theme) that is critical to the PDF generation process: prince-list.txt. This file simply iterates through the items in your sidebar and creates a list of links. Prince will consume the list of links from prince-list.txt and create a running PDF that contains all of the pages listed, with appropriate cross references and styling for them all.</p>

<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> If you have any files that you do not want to appear in the PDF, add <code>output: web</code> (rather than <code>output: pdf</code>) in the list of attributes in your sidebar. The prince-list.txt file that loops through the mydoc_sidebar.yml file to grab the URLs of each page that should appear in the PDF will skip over any items that do not list <code>output: pdf</code> in the item attributes. For example, you might not want your tag archives to appear in the PDF, but you probably will want to list them in the online help navigation.</div>

<h2 id="customize-your-headers-and-footers">4. Customize your headers and footers</h2>

<p>Open up the css/printstyles.css file and customize what you want for the headers and footers. At the very least, customize the email address (<code class="highlighter-rouge">youremail@domain.com</code>) that appears in the bottom left.</p>

<p>Exactly how the print styling works here is pretty nifty. You don’t need to understand the rest of the content in this section unless you want to customize your PDFs to look different from what I’ve configured. But I’m adding this information here in case you want to understand how to customize the look and feel of the PDF output.</p>

<p>This style creates a page reference for a link:</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">]</span><span class="nd">::after</span> <span class="p">{</span>
    <span class="nl">content</span><span class="p">:</span> <span class="s1">" (page "</span> <span class="n">target-counter</span><span class="p">(</span><span class="n">attr</span><span class="p">(</span><span class="n">href</span><span class="p">),</span> <span class="n">page</span><span class="p">)</span> <span class="s1">")"</span>
<span class="p">}</span>
</code></pre>
</div>

<p>You don’t want cross references for any link that doesn’t reference another page, so this style specifies that the content after should be blank:</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">*=</span><span class="s1">"mailto"</span><span class="o">]</span><span class="nd">::after</span><span class="o">,</span> <span class="nt">a</span><span class="o">[</span><span class="nt">data-toggle</span><span class="o">=</span><span class="s1">"tooltip"</span><span class="o">]</span><span class="nd">::after</span><span class="o">,</span> <span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">]</span><span class="nc">.noCrossRef</span><span class="nd">::after</span> <span class="p">{</span>
    <span class="nl">content</span><span class="p">:</span> <span class="s1">""</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>

<div class="alert alert-success" role="alert"><i class="fa fa-check-square-o"></i> <b>Tip:</b> If you have a link to a file download, or some other link that shouldn’t have a cross reference (such as link used in JavaScript for navtabs or collapsible sections, for example, add <code class="highlighter-rouge">noCrossRef</code> as a class to the link to avoid having it say “page 0” in the cross reference.</div>

<p>This style specifies that after links to web resources, the URL should be inserted instead of the page number:</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">^=</span><span class="s1">"http:"</span><span class="o">]</span><span class="nd">::after</span><span class="o">,</span> <span class="nt">a</span><span class="o">[</span><span class="nt">href</span><span class="o">^=</span><span class="s1">"https:"</span><span class="o">]</span><span class="nd">::after</span> <span class="p">{</span>
    <span class="nl">content</span><span class="p">:</span> <span class="s1">" ("</span> <span class="n">attr</span><span class="p">(</span><span class="n">href</span><span class="p">)</span> <span class="s1">")"</span><span class="p">;</span>
<span class="p">}</span>
</code></pre>
</div>

<p>This style sets the page margins:</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="k">@page</span> <span class="p">{</span>
    <span class="nl">margin</span><span class="p">:</span> <span class="m">60pt</span> <span class="m">90pt</span> <span class="m">60pt</span> <span class="m">90pt</span><span class="p">;</span>
    <span class="nl">font-family</span><span class="p">:</span> <span class="nb">sans-serif</span><span class="p">;</span>
    <span class="nl">font-style</span><span class="p">:</span><span class="nb">none</span><span class="p">;</span>
    <span class="nl">color</span><span class="p">:</span> <span class="no">gray</span><span class="p">;</span>

<span class="p">}</span>
</code></pre>
</div>

<p>To set a specific style property for a particular page, you have to name the page. This allows Prince to identify the page.</p>

<p>First you add frontmatter to the page that specifies the type. For the titlepage.html, here’s the frontmatter:</p>

<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="nn">---</span>
<span class="s">type</span><span class="pi">:</span> <span class="s">title</span>
<span class="nn">---</span>
</code></pre>
</div>

<p>For the tocpage, here’s the frontmatter:</p>

<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="nn">---</span>
<span class="s">type</span><span class="pi">:</span> <span class="s">frontmatter</span>
<span class="nn">---</span>
</code></pre>
</div>

<p>For the index.html page, we have this type tag (among others):</p>

<div class="language-yaml highlighter-rouge"><pre class="highlight"><code><span class="nn">---</span>
<span class="s">type</span><span class="pi">:</span> <span class="s">first_page</span>
<span class="nn">---</span>
</code></pre>
</div>

<p>The default_print.html layout will change the class of the <code class="highlighter-rouge">body</code> element based on the type value in the page’s frontmatter:</p>

<div class="language-liquid highlighter-rouge"><pre class="highlight"><code>&lt;body class="<span class="p">{%</span><span class="w"> </span><span class="kr">if</span><span class="w"> </span><span class="nv">page</span><span class="p">.</span><span class="nv">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"title"</span><span class="p">%}</span>title<span class="p">{%</span><span class="w"> </span><span class="kr">elsif</span><span class="w"> </span><span class="nv">page</span><span class="p">.</span><span class="nv">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"frontmatter"</span><span class="w"> </span><span class="p">%}</span>frontmatter<span class="p">{%</span><span class="w"> </span><span class="kr">elsif</span><span class="w"> </span><span class="nv">page</span><span class="p">.</span><span class="nv">type</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="s2">"first_page"</span><span class="w"> </span><span class="p">%}</span>first_page<span class="p">{%</span><span class="w"> </span><span class="kr">endif</span><span class="w"> </span><span class="p">%}</span> print"&gt;
</code></pre>
</div>

<p>Now in the css/printstyles.css file, you can assign a page name based on a specific class:</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="nt">body</span><span class="nc">.title</span> <span class="p">{</span> <span class="nl">page</span><span class="p">:</span> <span class="n">title</span> <span class="p">}</span>
</code></pre>
</div>

<p>This means that for content inside of <code class="highlighter-rouge">body class="title"</code>, we can style this page in our stylesheet using <code class="highlighter-rouge">@page title</code>.</p>

<p>Here’s how that title page is styled:</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="k">@page</span> <span class="n">title</span> <span class="p">{</span>
    <span class="k">@top-left</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">@top-right</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span>
    <span class="p">}</span>
    <span class="k">@bottom-right</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">@bottom-left</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="s1">" "</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p>As you can see, we don’t have any header or footer content, because it’s the title page.</p>

<p>For the tocpage.html, which has the <code class="highlighter-rouge">type: frontmatter</code>, this is specified in the stylesheet:</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="nt">body</span><span class="nc">.frontmatter</span> <span class="p">{</span> <span class="nl">page</span><span class="p">:</span> <span class="n">frontmatter</span> <span class="p">}</span>
<span class="nt">body</span><span class="nc">.frontmatter</span> <span class="p">{</span><span class="nl">counter-reset</span><span class="p">:</span> <span class="n">page</span> <span class="m">1</span><span class="p">}</span>


<span class="k">@page</span> <span class="n">frontmatter</span> <span class="p">{</span>
    <span class="k">@top-left</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">guideName</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">@top-right</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">datestamp</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">@bottom-right</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="n">counter</span><span class="p">(</span><span class="n">page</span><span class="p">,</span> <span class="nb">lower-roman</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">@bottom-left</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="s1">"youremail@domain.com"</span><span class="p">;</span>   <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p>With <code class="highlighter-rouge">counter(page, lower-roman)</code>, we reset the page count to 1 so that the title page doesn’t start the count. Then we also add some header and footer info. The page numbers start counting in lower-roman numerals.</p>

<p>Finally, for the first page (which doesn’t have a specific name), we restart the counting to 1 again and this time use regular numbers.</p>

<div class="language-css highlighter-rouge"><pre class="highlight"><code><span class="nt">body</span><span class="nc">.first_page</span> <span class="p">{</span><span class="nl">counter-reset</span><span class="p">:</span> <span class="n">page</span> <span class="m">1</span><span class="p">}</span>

<span class="nt">h1</span> <span class="p">{</span> <span class="nl">string-set</span><span class="p">:</span> <span class="n">doctitle</span> <span class="n">content</span><span class="p">()</span> <span class="p">}</span>

<span class="k">@page</span> <span class="p">{</span>
    <span class="k">@top-left</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="n">string</span><span class="p">(</span><span class="n">doctitle</span><span class="p">);</span>
        <span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
        <span class="nl">font-style</span><span class="p">:</span> <span class="nb">italic</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">@top-right</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">datestamp</span><span class="p">);</span>
        <span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="k">@bottom-right</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="s1">"Page "</span> <span class="n">counter</span><span class="p">(</span><span class="n">page</span><span class="p">);</span>
        <span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">@bottom-left</span> <span class="p">{</span>
        <span class="nl">content</span><span class="p">:</span> <span class="n">prince-script</span><span class="p">(</span><span class="n">guideName</span><span class="p">);</span>
        <span class="nl">font-size</span><span class="p">:</span> <span class="m">11px</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre>
</div>

<p>You’ll see some other items in there such as <code class="highlighter-rouge">prince-script</code>. This means we’re using JavaScript to run some functions to dynamically generate that content. These JavaScript functions are located in the _includes/head_print.html:</p>

<div class="language-js highlighter-rouge"><pre class="highlight"><code><span class="o">&lt;</span><span class="nx">script</span><span class="o">&gt;</span>
    <span class="nx">Prince</span><span class="p">.</span><span class="nx">addScriptFunc</span><span class="p">(</span><span class="s2">"datestamp"</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="s2">"PDF last generated: October 08, 2016"</span><span class="p">;</span>
    <span class="p">});</span>
<span class="o">&lt;</span><span class="sr">/script</span><span class="err">&gt;
</span>
<span class="o">&lt;</span><span class="nx">script</span><span class="o">&gt;</span>
    <span class="nx">Prince</span><span class="p">.</span><span class="nx">addScriptFunc</span><span class="p">(</span><span class="s2">"guideName"</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
        <span class="k">return</span> <span class="s2">" User Guide"</span><span class="p">;</span>
    <span class="p">});</span>
<span class="o">&lt;</span><span class="sr">/script</span><span class="err">&gt;
</span></code></pre>
</div>

<p>There are a couple of Prince functions that are default functions from Prince. This gets the heading title of the page:</p>

<div class="language-js highlighter-rouge"><pre class="highlight"><code>        <span class="nx">content</span><span class="err">:</span> <span class="nx">string</span><span class="p">(</span><span class="nx">doctitle</span><span class="p">);</span>
</code></pre>
</div>

<p>This gets the current page:</p>

<div class="language-js highlighter-rouge"><pre class="highlight"><code>        <span class="nx">content</span><span class="err">:</span> <span class="s2">"Page "</span> <span class="nx">counter</span><span class="p">(</span><span class="nx">page</span><span class="p">);</span>
</code></pre>
</div>

<p>Because the theme uses JavaScript in the CSS, you have to add the <code class="highlighter-rouge">--javascript</code> tag in the Prince command (detailed later on this page).</p>

<h2 id="customize-the-pdf-script">5. Customize the PDF script</h2>

<p>Duplicate the pdf-mydocf.sh file in the root directory and customize it for your specific configuration files.</p>

<div class="highlighter-rouge"><pre class="highlight"><code>echo 'Killing all Jekyll instances'
kill -9 $(ps aux | grep '[j]ekyll' | awk '{print $2}')
clear

echo "Building PDF-friendly HTML site for Mydoc ...";
jekyll serve --detach --config _config.yml,pdfconfigs/config_mydoc_pdf.yml;
echo "done";

echo "Building the PDF ...";
prince --javascript --input-list=_site/pdfconfigs/prince-list.txt -o _pdf/mydoc.pdf;
echo "done";
</code></pre>
</div>

<p>Note that the first part kills all Jekyll instances. This way you won’t try to serve Jekyll at a port that is already occupied.</p>

<p>The <code class="highlighter-rouge">jekyll serve</code> command serves up the HTML-friendly PDF configurations for our two projects. This web version is where Prince will go to get its content.</p>

<p>The prince script issues a command to the Prince utility. JavaScript is enabled (<code class="highlighter-rouge">--javascript</code>), and we tell it exactly where to find the list of files (<code class="highlighter-rouge">--input-list</code>) — just point to the prince-list.txt file. Then we tell it where and what to output (<code class="highlighter-rouge">-o</code>).</p>

<p>Make sure that the path to the prince-list.txt is correct. For the output directory, I like to output the PDF file into my project’s source (into the files folder). Then when I build the web output, the PDF is included and something I can refer to.</p>

<div class="alert alert-info" role="alert"><i class="fa fa-info-circle"></i> <b>Note:</b> You might not want to include the PDF in your project files, since you’re likely committing the PDF to Github and as a result saving the changes from one PDF version to another with each save.</div>

<h2 id="add-conditions-for-your-new-builds-in-the-sidebarconfigshtml-file">6. Add conditions for your new builds in the sidebarconfigs.html file</h2>

<p>In the _includes/custom/sidebarconfigs.html file, there’s a section that looks like this:</p>

<div class="highlighter-rouge"><pre class="highlight"><code><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">if</span><span class="w"> </span><span class="err">site.product</span><span class="w"> </span><span class="err">==</span><span class="w"> </span><span class="nt">"mydoc"</span><span class="w"> </span><span class="err">%}</span><span class="w">
</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">assign</span><span class="w"> </span><span class="err">sidebar_pdf</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">site.data.sidebars.mydoc_sidebar.entries</span><span class="w"> </span><span class="err">%</span><span class="p">}</span><span class="w">
</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">endif</span><span class="w"> </span><span class="err">%</span><span class="p">}</span><span class="w">

</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">if</span><span class="w"> </span><span class="err">site.product</span><span class="w"> </span><span class="err">==</span><span class="w"> </span><span class="nt">"product1"</span><span class="w"> </span><span class="err">%}</span><span class="w">
</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">assign</span><span class="w"> </span><span class="err">sidebar_pdf</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">site.data.sidebars.product1_sidebar.entries</span><span class="w"> </span><span class="err">%</span><span class="p">}</span><span class="w">
</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">endif</span><span class="w"> </span><span class="err">%</span><span class="p">}</span><span class="w">

</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">if</span><span class="w"> </span><span class="err">site.product</span><span class="w"> </span><span class="err">==</span><span class="w"> </span><span class="nt">"product2"</span><span class="w"> </span><span class="err">%}</span><span class="w">
</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">assign</span><span class="w"> </span><span class="err">sidebar_pdf</span><span class="w"> </span><span class="err">=</span><span class="w"> </span><span class="err">site.data.sidebars.product2_sidebar.entries</span><span class="w"> </span><span class="err">%</span><span class="p">}</span><span class="w">
</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">endif</span><span class="w"> </span><span class="err">%</span><span class="p">}</span><span class="w">
</span></code></pre>
</div>

<p>Add your own condition here that points to your sidebar.</p>

<p>What this does is allow the prince-list.txt and toc.html files to use a variable for the sidebar (called <code class="highlighter-rouge">sidebar_pdf</code>) when iterating through the sidebar. Otherwise, you would need to create a unique prince-list.txt and toc.html file for each separate PDF output you have.</p>

<h2 id="add-a-download-button-for-the-pdf">7. Add a download button for the PDF</h2>

<p>You can add a download button for your PDF using some Bootstrap button code:</p>

<div class="language-html highlighter-rouge"><pre class="highlight"><code><span class="nt">&lt;a</span> <span class="na">target=</span><span class="s">"_blank"</span> <span class="na">class=</span><span class="s">"noCrossRef"</span> <span class="na">href=</span><span class="s">"/pdf/mydoc.pdf"</span><span class="nt">&gt;&lt;button</span> <span class="na">type=</span><span class="s">"button"</span> <span class="na">class=</span><span class="s">"btn btn-default"</span> <span class="na">aria-label=</span><span class="s">"Left Align"</span><span class="nt">&gt;&lt;span</span> <span class="na">class=</span><span class="s">"glyphicon glyphicon-download-alt"</span> <span class="na">aria-hidden=</span><span class="s">"true"</span><span class="nt">&gt;&lt;/span&gt;</span> PDF Download<span class="nt">&lt;/button&gt;&lt;/a&gt;</span>
</code></pre>
</div>

<p>Here’s what that looks like:</p>

<p>&lt;a target=”_blank” class=”noCrossRef” href=pdf/mydoc.pdf”&gt;<button type="button" class="btn btn-default" aria-label="Left Align"><span class="glyphicon glyphicon-download-alt" aria-hidden="true"></span> PDF Download</button>&lt;/a&gt;</p>

<h2 id="javascript-conflicts">JavaScript conflicts</h2>

<p>If you have JavaScript on any of your pages, Prince will note errors in Terminal like this:</p>

<div class="highlighter-rouge"><pre class="highlight"><code>error: TypeError: value is not an object
</code></pre>
</div>

<p>However, the PDF will still build.</p>

<p>You need to conditionalize out any JavaScript from your PDF web output before building your PDFs. Make sure that the PDF configuration files have the <code class="highlighter-rouge">output: pdf</code> property.</p>

<p>Then surround the JavaScript with conditional tags like this:</p>

<div class="highlighter-rouge"><pre class="highlight"><code><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">unless</span><span class="w"> </span><span class="err">site.output</span><span class="w"> </span><span class="err">==</span><span class="w"> </span><span class="nt">"pdf"</span><span class="w"> </span><span class="err">%}</span><span class="w">
</span><span class="err">javascript</span><span class="w"> </span><span class="err">content</span><span class="w"> </span><span class="err">here</span><span class="w"> </span><span class="err">...</span><span class="w">
</span><span class="p">{</span><span class="err">%</span><span class="w"> </span><span class="err">endunless</span><span class="w"> </span><span class="err">%</span><span class="p">}</span><span class="w">
</span></code></pre>
</div>

<p>For more detail about using <code class="highlighter-rouge">unless</code> in conditional logic, see <a href="mydoc_conditional_logic.html">Conditional logic</a>. What this code means is “run this code unless this value is the case.”</p>

<h2 id="overriding-bootstrap-print-styles">Overriding Bootstrap Print Styles</h2>

<p>The theme relies on Bootstrap’s CSS for styling. However, for print media, Bootstrap applies the following style:</p>

<div class="highlighter-rouge"><pre class="highlight"><code>@media print{*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}
</code></pre>
</div>
<p>This is minified, but basically the <code class="highlighter-rouge">*</code> (asterisk) means select all, and applied the color #000 (black). As a result, the Bootstrap style strips out all color from the PDF (for Bootstrap elements).</p>

<p>This is problematic for code snippets that have syntax highlighting. I decided to remove this de-coloring from the print output. I commented out the Bootstrap style:</p>

<div class="highlighter-rouge"><pre class="highlight"><code>@media print{*,:after,:before{/*color:#000!important;*/text-shadow:none!important;/*background:0 0!important*/;-webkit-box-shadow:none!important;box-shadow:none!important}
</code></pre>
</div>

<p>If you update Bootrap, make sure you make this edit. (Sorry, admittedly I couldn’t figure out how to simply overwrite the <code class="highlighter-rouge">*</code> selector with a later style.)</p>

<p>I did, however, remove the color from the alerts and lighten the background shading for <code class="highlighter-rouge">pre</code> elements. The printstyles.css has this setting.</p>



    <div class="tags">
        
        <b>Tags: </b>
        
        
        
        <a href="tag_publishing.html" class="btn btn-default navbar-btn cursorNorm" role="button">publishing</a>
        
        
        
        <a href="tag_single_sourcing.html" class="btn btn-default navbar-btn cursorNorm" role="button">single_sourcing</a>
        
        
        
        <a href="tag_content_types.html" class="btn btn-default navbar-btn cursorNorm" role="button">content_types</a>
        
        
        
    </div>

    

</div>

<hr class="shaded"/>

<footer>
            <div class="row">
                <div class="col-lg-12 footer">
               &copy;2016 . All rights reserved. <br />
<span>Page last updated:</span> July 3, 2016<br/> Site last generated: Oct 8, 2016 <br />
<!-- <p><img src="images/company_logo.png" alt="Company logo"/></p> -->
                </div>
            </div>
</footer>


    </div>
    <!-- /.row -->
</div>
<!-- /.container -->
    </div>

</body>

</html>
